package com.le.tester.imooc.oldfairy.dbgen;

import db.gen.Chapter;

import java.io.*;
import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.IntStream;

/**
 * createTime：2022/1/11 15:53
 * description：BookLoader
 */
public class BookLoader {
    public void anotherLoad() {
        IntStream.range(1, 121)
                .mapToObj(this::getChapterResource)
                .collect(LinkedBlockingQueue::new, AbstractQueue::add, AbstractQueue::addAll);
    }

    private Resource<Chapter> getChapterResource(int x) {
        //红楼梦章节信息
        return new Resource<>(
                String.format("http://www.purepen.com/hlm/%03d.htm", x),
                x);
    }

    public static class Sentence {
        String text;
        int chapterId;
        int offset;

        public String getText() {
            return text;
        }

        public int getChapterId() {
            return chapterId;
        }
    }

    public ArrayList<Sentence> sentences() throws IOException, ClassNotFoundException {
        ArrayList<Sentence> arr = new ArrayList<>();
        for (int i = 1; i < 120; i++) {
            //读取文件
            File file = new File(String.format("E:\\L.info\\L.Application\\L.Tester\\src\\main\\java\\" +
                    "com\\le\\tester\\imooc\\oldfairy\\data\\novel\\%d.txt", i));
            ObjectInputStream fin = new ObjectInputStream(new FileInputStream(file));
            Chapter chapter = (Chapter) fin.readObject();
            String[] sens = chapter.content.split("。");
            int sensLen = sens.length;
            for (int j = 0; j < sensLen; j++) {
                Sentence sentence = new Sentence();
                sentence.chapterId = i;
                sentence.text = sens[j].replaceAll("\n", "");
                arr.add(sentence);
            }
        }
        return arr;
    }
}
